package com.example.springbootshiro.config;

import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;

public class UserRealm extends AuthorizingRealm {
    @Override//授权
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        System.out.println("执行授权----->>");
        return null;
    }

    @Override//认证
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        System.out.println("执行认证----->>");
        //用户名，密码从数据库中取
        String name="wang";
        String pass="123";
        UsernamePasswordToken token = (UsernamePasswordToken) authenticationToken;

        if(!token.getUsername().equals(name)){
            return null; //抛出异常  UnknownAccountException 用户名报错异常
        }                   //密码不用自己判断  shrio内部已经判断
        return new SimpleAuthenticationInfo("",pass,"");
    }
}
